<script setup>
import { ref } from 'vue'
import {artAddChannelService, artEditChannelService} from "@/api/article.js";
// defineProps({
//   title: {
//     type: String,
//     default: '新增'
//   }
// })
const dialogTableVisible = ref(false)

// 打开窗口
const open = (row) => {
  dialogTableVisible.value = true
  // 编辑回显
  formModel.value = { ... row }
}
// 让父组件获取组件实例
defineExpose({
  open
})

const formModel = ref({
  cate_name: '',
  cate_alias: ''
})
const rules = {
  cate_name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' },
    {
      pattern: /^\S{1,10}$/,
      message: '分类名必须是1-10位的非空字符',
      trigger: 'blur'
    }
  ],
  cate_alias: [
    { required: true, message: '请输入分类别名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{1,15}$/,
      message: '分类别名必须是1-15位的字母数字',
      trigger: 'blur'
    }
  ]
}
const rulesForm = ref(null)
const emit = defineEmits(['success'])
const onSubmit = async () => {
   await rulesForm.value.validate()
  // 判断是新增还是判断
  // formModel.value.id ? await artEditChannelService(formModel.value): await artAddChannelService(formModel.value)
  ElMessage({
    message: formModel.value.id ? '编辑成功' : '新增成功' ,
    type: 'success',
  })
  dialogTableVisible.value = false
  emit('success')
}
</script>

<template>
  <el-dialog v-model="dialogTableVisible" :title="formModel.id ? '编辑分类' : '新增分类'" width="30%">
    <el-form
      :model="formModel"
      :rules="rules"
      label-width="100px"
      style="padding-right: 30px"
      ref="rulesForm"
    >
      <el-form-item label="分类名称" prop="cate_name">
        <el-input
          v-model="formModel.cate_name"
          minlength="1"
          maxlength="10"
        ></el-input>
      </el-form-item>
      <el-form-item label="分类别名" prop="cate_alias">
        <el-input
          v-model="formModel.cate_alias"
          minlength="1"
          maxlength="15"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>
